<?php

class clsTemplates	{
	
	var $templateFolder = '';
	var $templateExtend = '.html';
	var $templateName ;
	# Store templates block content
	var $templatesBlock = array();
	var $templatesComponen = array();
	var $hasError = array();
	var $templateContent ;
	
	function clsTemplates($fileName = '', $autogetblock = false){
		if($fileName) {
			# Asign information
			$this->templateName = $fileName;
			# Get template content
			if($this->getTemplateContent() && $autogetblock) {
				# Get templates block			
				$this->getTemplatesBlock();
			}
		}
	}
	
	
	# Get templates
	# Read templates file url
	# Return templates content
	function getTemplateContent(){
		$strTemplatesFName = $this->templateName;
		if(!preg_match('/^[a-zA-Z0-9]+$/',$strTemplatesFName)) {
			$this->hasError['str'] = "Lỗi tên templates";
			return false ;
		}
		$fileName = $this->templateFolder.$strTemplatesFName.$this->templateExtend ;
		$i = 0 ;
		for ($i = 0; $i<3; $i++) {
			if( @file_exists($fileName) )	{
				$CF = @fopen ($fileName, "r");
				$get = @fread ($CF,@filesize ($fileName));
				@fclose ($CF);
				$this->templateContent = $get ;
				return true;
			}
		}
		$this->hasError['str'] = "Không đọc được template";
		return false ;
	}
	
	function getTemplatesBlock(){
		$str = $this->templateContent;
		preg_match_all('#<!-- \#B (.*?) -->[\r\n]*(.*?)[\r\n]*<!-- \#E (.*?) -->#s', $str, $arr, PREG_PATTERN_ORDER);
		$a = array();
		for ($i=0; $i<count($arr[0]); $i++) {
			$a[$arr[1][$i]] = $arr[0][$i];
		}
		$this->templatesBlock = $a;
		preg_match_all('#<!-- BOC (.*?)\((.*?)\) -->(.*?)<!-- EOC (.*?) -->#se',$str,$arr, PREG_PATTERN_ORDER);	//EOC
		$a = array();
		for ($i=0; $i<count($arr[0]); $i++) {
			$a[$arr[1][$i]] = $arr[0][$i];
		}
		$this->templatesComponen = $a;
	}
	
	
	# Get block content
	function getBlock($blockName){
		//return $this->templatesBlock[$blockName];
		$str = $this->templateContent;
		preg_match_all('#<!-- \#B '.$blockName.' -->[\r\n]*(.*?)[\r\n]*<!-- \#E '.$blockName.' -->#s', $str, $arr, PREG_PATTERN_ORDER);
		return $arr[1][0];
	}
	
	function getComp($compName){
		return $this->templatesComponen[$compName];
	}
	
	function replaceBlock($blockName,$stringContent,$code){
		return preg_replace('#<!-- \#B '.$blockName.' -->[\r\n]*(.*?)[\r\n]*<!-- \#E '.$blockName.' -->#s',$stringContent, $code);
	}
	
	# Auto get function templates
	function parseComponent($func,$exp = '') {
		$exp = trim(stripslashes($exp));
		if ($exp) $code = eval("return ".$func."(".$exp.");");
		else $code = eval("return ".$func."();"); 
		return $code;
	}
	
	
	# Replace element function
	function replace($source,$inp = array()){
		foreach($inp as $key => $value ) {
			//$source = str_replace('{'.$key.'}',$value,$source);
			$source = str_replace($key,$value,$source);
		}
		return $source;
	}
	
/*	public static function getBlockContent($source){
		
	}*/
	
	# Auto parse templates
	function parseTeplates() {
		$code = $this->templateContent;
		if(!$code) return '';
		$code = preg_replace('#<!-- BOC (.*?)\((.*?)\) -->(.*?)<!-- EOC (.*?) -->#se', '$this->parseComponent("\\1","\\2","\\3");', $code);	//EOC
		/*$code = str_replace('{TPL_LINK}', "templates/".$_SESSION['current_tpl'], $code);
		$code = str_replace('{WEB_TITLE}', $webTitle, $code);
		$code = preg_replace('#<!-- BEGIN (.*?) -->[\r\n]*(.*?)[\r\n]*<!-- END (.*?) -->#s', '\\2', $code);*/
		return $code;
	}
	
}


class clsTable {
	
	var $name ;
	var $columns = array();
	var $data = array();
	
 
	
	function clsTable($name = "",$columns = array()){
		 $this->name = $name;
		 $this->columns = $columns;
	}
	
	function addColumns($content = array()){
		$this->columns = $content;
	}
	
	function putInit($inc = false){
		if($inc) $include_js = ($inc)?'<script type="text/javascript" language="javascript" src="../jsframework/jquery.dataTables.js"></script>':"";
		$ret = $include_js .'
		<script type="text/javascript" charset="utf-8">
			$(document).ready(function() { 
				$("body").ajaxError(function(event,request,setting){
					alert("Thông báo lỗi truyền dữ liệu máy chủ \nTrang : " + setting.url + "\nVui lòng thử lại");
				});	
				oTable = $("#'.$this->name.'").dataTable({
					"bJQueryUI": true,
					"sPaginationType": "full_numbers",
					"aoColumns": [ ';
		foreach($this->columns as $key => $value ) {			  
			if($key['unsort']) $ret .= '{ bSortable: false },';
			else $ret .= 'null,';
		}
		$ret .= '	]
				});
			} );
		</script>';
		
		return $ret;
	}

	function addRow($id,$content = array()){
		$this->data[$id] = $content;
	}
	
	function putData(){
		//FB::info($this->data);
		$ret = '<table cellpadding="0" cellspacing="0" border="0" class="display" id="'.$this->name.'">
				<thead>
				<tr>';
		// Put header
		foreach($this->columns as $key => $value ) {			  
			$ret .= '<th><strong>'.$value[0].'</strong></th>';
		}
		$ret .= '</tr></thead><tbody>';
		// Put content
		foreach($this->data as $key => $value ) 
		{	
			$ret .= '<tr>';
			$i = 0;
			foreach($value as $content)
			{
				if($this->columns[$i][1] == 1)
					$ret .= '<td align="center">';
				else if($this->columns[$i][1] == 2)
					$ret .= '<td align="right">';
				else
					$ret .= '<td>';
				$ret .= $content.'</td>';
				$i++;
			}
			$ret .= '</tr>';			
		}
		$ret .= '</tbody>';
		return $ret;
	}
	
	function putHTML($inc){
		return $this->putInit($inc).$this->putData();
	}
	
}




?>